//
//  NSDate+QA.m
//  TravelGuideMdd
//
//  Created by 刘澈 on 14-8-4.
//  Copyright (c) 2014年 mafengwo.com. All rights reserved.
//

#import "NSDate+QA.h"

#define SECOND  1
#define MINUTE  (SECOND * 60)
#define HOUR    (MINUTE * 60)
#define DAY     (HOUR   * 24)
#define WEEK    (DAY    * 7)
#define MONTH   (DAY    * 31)
#define YEAR    (DAY    * 365.24)

@implementation NSDate (QA)


+ (NSString *)filterDate2:(NSDate *)adate{
    return  [adate getTime];
}

+ (NSString *)timeStringWithDateFormatter:(NSString *)dateFormatter{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[dateFormatter floatValue]];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

- (NSString *)getTime{
    NSDate *now = [NSDate date];
    if ([self isSameDayAs:now]){
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        NSString *currentDateStr = [dateFormatter stringFromDate:self];
       return currentDateStr;
    }
    
    NSCalendarUnit units = NSCalendarUnitDay | NSCalendarUnitWeekOfYear |
    NSCalendarUnitMonth | NSCalendarUnitYear;
    
    // if `date` is before "now" (i.e. in the past) then the components will be positive
    NSDateComponents *components = [[NSCalendar currentCalendar] components:units
                                                                   fromDate:self
                                                                     toDate:[NSDate date]
                                                                    options:0];
    if (components.year == 0 && components.month == 0 && components.day < 1){
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        NSString *currentDateStr = [dateFormatter stringFromDate:self];
        return [NSString stringWithFormat:@"昨天 %@",currentDateStr];
    }
    if (components == 0){
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM月dd日 HH:mm"];
        NSString *currentDateStr = [dateFormatter stringFromDate:self];
        return currentDateStr;
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY年MM月dd日 HH:mm"];
    NSString *currentDateStr = [dateFormatter stringFromDate:self];
    return currentDateStr;
}

+(NSString *)filterDate:(NSDate *)adate{
    
    /*
     NSDateFormatter *format=[[NSDateFormatter alloc] init];
     [format setDateFormat:@"yyyy-MM-dd"];
     NSDate *createDate = [format dateFromString:[format stringFromDate:adate]];
     NSDate *nowDate = [format dateFromString:[format stringFromDate:[NSDate date]]];
     NSTimeInterval interval =  [nowDate timeIntervalSince1970] - [createDate timeIntervalSince1970];
     int day = interval/(3600 *24);
     if (day < 0) {
     return [format stringFromDate:adate];
     }
     if(day == 0){
     return @"今天";
     }else if(day == 1){
     return @"昨天";
     }else if (day >1 && day<7){
     return [NSString stringWithFormat:@"%d天前",day];
     }else{
     [format setDateFormat:@"yyyy.MM.dd"];
     return [format stringFromDate:adate];
     }
     */
    return   [adate formattedAsTimeAgo];
}


/*
 Formatted As Time Ago
 Returns the date formatted as Time Ago (in the style of the mobile time ago date formatting for Facebook)
 */
- (NSString *)formattedAsTimeAgo
{
    //Now
    NSDate *now = [NSDate date];
    NSTimeInterval secondsSince = -(int)[self timeIntervalSinceDate:now];
    
    //Should never hit this but handle the future case
    if(secondsSince < 0){
        return @"来自未来";
    }
    
    // < 1 minute = "Just now"
    if(secondsSince < MINUTE){
        return @"刚刚";
    }
    
    // Yesterday = "Yesterday at 1:28 PM"
    if([self isYesterday:now]){
        return @"昨天";
    }
    
    // < 1 hour = "x minutes ago"
    if(secondsSince < HOUR){
        return [self formatMinutesAgo:secondsSince];
    }
    
    // Today = "x hours ago"
    if(secondsSince < DAY){
        return [self formatAsToday:secondsSince];
    
    }
    
    
    if(secondsSince < MONTH){
        return [self formatDaysAgo:secondsSince];
    }

//    
//    if(secondsSince < YEAR){
//        return [self formatMonthAgo:secondsSince];
//    }
    
    
    return [self formatAsOther];
}




/*
 ========================== Date Comparison Methods ==========================
 */

/*
 Is Same Day As
 Checks to see if the dates are the same calendar day
 */
- (BOOL)isSameDayAs:(NSDate *)comparisonDate
{
    //Check by matching the date strings
    NSDateFormatter *dateComparisonFormatter = [[NSDateFormatter alloc] init];
    [dateComparisonFormatter setDateFormat:@"yyyy-MM-dd"];
    
    //Return true if they are the same
    return [[dateComparisonFormatter stringFromDate:self] isEqualToString:[dateComparisonFormatter stringFromDate:comparisonDate]];
}




/*
 If the current date is yesterday relative to now
 Pasing in now to be more accurate (time shift during execution) in the calculations
 */
- (BOOL)isYesterday:(NSDate *)now
{
    return [self isSameDayAs:[now dateBySubtractingDays:1]];
}


//From https://github.com/erica/NSDate-Extensions/blob/master/NSDate-Utilities.m
- (NSDate *) dateBySubtractingDays: (NSInteger) numDays
{
	NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + DAY * -numDays;
	NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
	return newDate;
}





// < 1 hour = "x minutes ago"
- (NSString *)formatMinutesAgo:(NSTimeInterval)secondsSince
{
    //Convert to minutes
    int minutesSince = (int)secondsSince / MINUTE;
    
    //Handle Plural
    if(minutesSince == 1)
        return @"1分钟前";
    else
        return [NSString stringWithFormat:@"%d分钟前", minutesSince];
}


// Today = "x hours ago"
- (NSString *)formatAsToday:(NSTimeInterval)secondsSince
{
    //Convert to hours
    int hoursSince = (int)secondsSince / HOUR;
    
    //Handle Plural
    if(hoursSince == 1)
        return @"1小时前";
    else
        return [NSString stringWithFormat:@"%d小时前", hoursSince];
}


- (NSString *)formatDaysAgo:(NSTimeInterval)secondsSince
{
    //Convert to hours
    int since = (int)secondsSince / DAY;
    
    //Handle Plural
    if(since == 1)
        return @"1天前";
    else
        return [NSString stringWithFormat:@"%d天前", since];
}

- (NSString *)formatMonthAgo:(NSTimeInterval)secondsSince
{
    //Convert to hours
    int since = (int)secondsSince / MONTH;
    
    //Handle Plural
    if(since == 1)
        return @"1个月前";
    else
        return [NSString stringWithFormat:@"%d个月前", since];
}



// Anything else = "September 9, 2011"
- (NSString *)formatAsOther
{
    //Create date formatter
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
    
    //Format
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter stringFromDate:self];
}





@end
